<?php
namespace app\beautiful\controller\admin;
use app\beautiful\model\CouponModel;
use app\BaseController;
use app\beautiful\model\project\ProjectTypeModel;
use app\common\logic\DataLogic;

class Coupon extends BaseController{
    protected $name='优惠券';

    public function index()
    {
        $status=$this->request->param('status/d',1);
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        $coupon=new CouponModel();
        $today=date("Y-m-d",$this->request->time());
        if($status==1) $where[]=['status','=',1];
        if($status==1) $where[]=['end_time','>=',$today];
        if($status==2) $coupon=$coupon->whereRaw("status = -1 OR ( end_time < '{$today}')");
        $order=['update_time'=>'desc','add_time'=>'desc'];
        $field="coupon_id,name,valid_bg_time,valid_end_time,coupon_money,money_limit,add_time";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($coupon,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $coupon_id=$this->request->param('coupon_id/d',0);
        if($coupon_id==0){
            return $this->success([]);
        }
        if(null === $coupon= CouponModel::find($coupon_id)){
            return $this->error($this->name.'不存在');
        }
        if($coupon->is_delete!=0){
            return $this->error($this->name.'不存在');
        }
        if($coupon->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        $coupon->append(['release_time','validity_time']);
        $detail=[
            'name'=>$coupon->name,
            'type_id'=>$coupon->type_id,
            'release_time'=>$coupon->release_time,
            'validity_time'=>$coupon->validity_time,
            'coupon_money'=>$coupon->coupon_money,
            'money_limit'=>$coupon->money_limit,
            'is_integral_exchange'=>$coupon->is_integral_exchange,
            'exchange_integral'=>$coupon->exchange_integral,
            'quota'=>$coupon->quota,
            'project_type_id'=>$coupon->project_type_id,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $coupon_id=$this->request->param('coupon_id/d',0);
        $name=$this->request->param('name');
        $type_id=$this->request->param('type_id/d',0);
        $release_time=$this->request->param('release_time/a',[]);
        $validity_time=$this->request->param('validity_time/a',[]);
        $coupon_money=$this->request->param('coupon_money/f',0);
        $money_limit=$this->request->param('money_limit/f',0);
        $is_integral_exchange=$this->request->param('is_integral_exchange/d',0);
        $exchange_integral=$this->request->param('exchange_integral/d',0);
        $quota=$this->request->param('quota/d',0);
        $project_type_id=$this->request->param('project_type_id/d',0);
        if(empty($name) || mb_strlen($name)>20){
            return $this->error('名称必须填写且长度不能大于20');
        }
        if(!in_array($type_id,[1,2,3,4])){
            return $this->error('请选择分类');
        }
        //开放时间
        if(empty($release_time) ||count($release_time) != 2){
           return $this->error('请选择开放时间');
        }
        $start_time=$release_time[0];
        $end_time=$release_time[1];
        if(false === $start_time_unix = strtotime($start_time)){
           return $this->error('请选择正确的开放开始时间');
        }
        if(false === $end_time_unix = strtotime($end_time)){
           return $this->error('请选择正确的开放结束时间');
        }
        if($start_time<date("Y-m-d",$this->request->time())){
            return $this->error('开放开始时间不能小于当前时间');
        }
        if($end_time_unix<$start_time_unix){
           return $this->error('开放开始时间不能大于结束时间');
        }

        //有效时间
        if(empty($validity_time) ||count($validity_time) != 2){
            return $this->error('请选择有效时间');
        }
        $valid_bg_time=$validity_time[0];
        $valid_end_time=$validity_time[1];
        if(false === $valid_bg_time_unix = strtotime($valid_bg_time)){
            return $this->error('请选择正确的有效开始时间');
        }
        if(false === $valid_end_time_unix = strtotime($valid_end_time)){
            return $this->error('请选择正确的有效结束时间');
        }
        if($valid_bg_time<date("Y-m-d",$this->request->time())){
            return $this->error('有效开始时间不能小于当前时间');
        }
        if($valid_end_time_unix<$valid_bg_time_unix){
            return $this->error('有效开始时间不能大于结束时间');
        }

        if($valid_bg_time_unix<$start_time_unix){
            return $this->error('有效开始时间不能小于开放开始时间');
        }
        if($valid_end_time_unix<$end_time_unix){
            return $this->error('有效结束时间不能小于开放结束时间');
        }

        if(empty($coupon_money) || $coupon_money<=0){
            return $this->error('优惠金额需要填写且不能小于0');
        }
        if(empty($money_limit) || $money_limit<=0){
            return $this->error('满减条件金额需要填写且不能小于0');
        }
        if($money_limit<$coupon_money){
            return $this->error('满减条件金额不能小于优惠金额');
        }

        if(in_array($type_id,[1,3])){
            if(!in_array($is_integral_exchange,[0,1])){
                return $this->error('请选择是否使用积分兑换');
            }
            if($is_integral_exchange==1){
                if(empty($exchange_integral) || $exchange_integral<=0){
                    return $this->error('请输入兑换积分且积分不能小于0');
                }
            }
            if(empty($quota) || $quota<=0){
                return $this->error('请输入配额，且配额不能小于0');
            }
        }

        if(is_numeric($project_type_id) && $project_type_id!=0 && ProjectTypeModel::find($project_type_id)==null){
            return $this->error('指定的类目已不存在');
        }

        if($coupon_id != 0){
            if(null === $coupon = CouponModel::find($coupon_id)){
                return $this->error($this->name.'不存在');
            }
            if($coupon->is_delete != 0){
                return $this->error($this->name.'不存在');
            }
            if($coupon->shop_id != $this->request->shopId){
                return $this->error($this->name.'不存在');
            }
        }else{
            $coupon = new CouponModel();
            $coupon->shop_id = $this->request->shopId;
        }
        $coupon->status=1;
        $coupon->update_time=$this->request->time();
        $coupon->name=$name;
        $coupon->type_id=$type_id;
        $coupon->start_time=$start_time;
        $coupon->end_time=$end_time;
        $coupon->valid_bg_time=$valid_bg_time;
        $coupon->valid_end_time=$valid_end_time;
        $coupon->coupon_money=$coupon_money;
        $coupon->money_limit=$money_limit;
        $coupon->project_type_id=$project_type_id;
        if(in_array($type_id,[1,3])){
            $coupon->is_integral_exchange=$is_integral_exchange;
            $coupon->exchange_integral=$exchange_integral;
            $coupon->quota=$quota;
        }
        $coupon->save();
        return $this->success(['id'=>$coupon->coupon_id]);
    }

    public function undercarriage()
    {
        $coupon_id=$this->request->param('coupon_id/d',0);
        if(null === $coupon = CouponModel::find($coupon_id)){
            return $this->error($this->name.'不存在');
        }

        if($coupon->is_delete !=0){
            return $this->error($this->name.'不存在');
        }

        if($coupon->shop_id !=$this->request->shopId){
            return $this->error($this->name.'不存在');
        }

        if($coupon->status !=1){
            return $this->error($this->name.'已下架，请勿重复操作');
        }

        $coupon->status=-1;
        $coupon->save();
        return $this->success('下架成功');
    }

    public function grounding()
    {
        $coupon_id=$this->request->param('coupon_id/d',0);
        if(null === $coupon = CouponModel::find($coupon_id)){
            return $this->error($this->name.'不存在');
        }

        if($coupon->is_delete !=0){
            return $this->error($this->name.'不存在');
        }

        if($coupon->shop_id !=$this->request->shopId){
            return $this->error($this->name.'不存在');
        }

        if($coupon->status !=-1){
            return $this->error($this->name.'已上架，请勿重复操作');
        }

        $coupon->status=1;
        $coupon->save();
        return $this->success('上架成功');
    }

    public function del()
    {
        $coupon_id=$this->request->param('coupon_id/d',0);
        if(null === $coupon = CouponModel::find($coupon_id)){
            return $this->error($this->name.'不存在');
        }

        if($coupon->is_delete !=0){
            return $this->error($this->name.'不存在');
        }

        if($coupon->shop_id !=$this->request->shopId){
            return $this->error($this->name.'不存在');
        }

        $today=date("Y-m-d",$this->request->time());
        if($coupon->end_time >=$today){
            if($coupon->status !=-1){
                return $this->error($this->name.'未下架，不可删除');
            }
        }

        $coupon->status=-1;
        $coupon->is_delete=1;
        $coupon->save();
        return $this->success('删除成功');
    }

    public function choose()
    {
        $type=$this->request->param('type');
        $keyword = $this->request->param('keyword');
        $today=date("Y-m-d",$this->request->time());
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['status','=',1],
            ['end_time','>=',$today]
        ];
        if($type=='setting_balance') $where[]=['type_id','in',[1,3]];

        if(!empty($keyword)) $where[]=['name','like','%'.$keyword.'%'];

        $coupon=new CouponModel();
        $order=['update_time'=>'desc','add_time'=>'desc'];
        $field="coupon_id,name,valid_bg_time,valid_end_time,coupon_money,money_limit,add_time";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($coupon,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }
}
